package com.tjetc.Impl;

import com.tjetc.domain.Doctormessge;
import com.tjetc.domain.Newborn;
import com.tjetc.domain.Performance;
import com.tjetc.mapper.DoctormessgeMapper;
import com.tjetc.mapper.NewbornMapper;
import com.tjetc.mapper.PerformanceMapper;
import com.tjetc.mapper.VaccinationMapper;
import com.tjetc.service.DoctorMessageService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class DoctorMessageServiceImpl implements DoctorMessageService {
    private Result result=new Result();
    private DoctormessgeMapper doctormessgeMapper= Db.getMapper(DoctormessgeMapper.class);
    private NewbornMapper newbornMapper=Db.getMapper(NewbornMapper.class);
    private VaccinationMapper vaccinationMapper=Db.getMapper(VaccinationMapper.class);
    private PerformanceMapper performanceMapper=Db.getMapper(PerformanceMapper.class);
    @Override
    public List<Newborn> sel(int id) {
        return doctormessgeMapper.sel(id);
    }
    @Override
    public Performance selPerson(int id) {
        // 统计护理新生儿数量
        int newbornCount = newbornMapper.countNewbornByDoctorId(id);
        // 统计已接种疫苗数量
        int vaccinationCount = vaccinationMapper.countVaccinationByDoctorId(id);
        Performance performance = new Performance();
        performance.setNewborncount(newbornCount);
        performance.setVaccination(vaccinationCount);
        performance.setDoctorId(id);
        try {
            performanceMapper.insert(performance);
        } catch (Exception e) {

        }

        return performance;
    }

    @Override
    public Result add(Doctormessge doctormessge) {
        try {
            Doctormessge doctormessge1=doctormessgeMapper.findBDI(doctormessge.getDoctorId());
            if (doctormessge1!=null){
                return result.setCode(500).setMsg("已经补充过信息,请点击修改");
            }else {
                int rows=doctormessgeMapper.insert(doctormessge);
                return rows==1?result.setCode(200).setMsg("补充成功"):result.setCode(500).setMsg("补充失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("补充失败");
    }

    @Override
    public Result upd(Doctormessge doctormessge) {
        try {
            Doctormessge doctormessge1=doctormessgeMapper.selectByPrimaryKey(doctormessge.getDoctorId());
            System.out.println(doctormessge1);
            if (doctormessge1==null){
                return result.setCode(500).setMsg("不存在该信息");
            }else {
                int rows=doctormessgeMapper.updateByPrimaryKey(doctormessge);
                return rows==1?result.setCode(200).setMsg("修改成功"):result.setCode(500).setMsg("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("修改失败");
    }

    @Override
    public Doctormessge selP(int doctorId) {
        return doctormessgeMapper.selectByPrimaryKey(doctorId);
    }

    @Override
    public List<Doctormessge> list() {
        return doctormessgeMapper.selP();
    }

}
